﻿using Dao;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Order : System.Web.UI.Page
{

    public string OrderInfor { get; set; }
    private OrderDao OrderDao = new OrderDao();
    StateDao stateDao = new StateDao();
    private Account account;

    protected void Page_Load(object sender, EventArgs e)
    {
        // 验证当前是否有用户登录
        account = (Account)Session["account"];
        // 未登录则禁止访问本页，跳转到登录页面
        if (account == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {

            // 通过用户ID获取订单
            List<Model.Order> list = OrderDao.GetOrdersByAccountId(account.Id);

            ReflashPage(list);
        }
    }


    protected void ReflashPage(List<Model.Order> list)
    {
        if (list != null)
        {
            // 套接字符串
            StringBuilder sbBuilder = new StringBuilder();
            foreach (Model.Order order in list)
            {
                sbBuilder.AppendFormat(
                    "<div class ='page_bookDetail' >" +
                    "<div class='row'>" +
                    " <div class='col-md-3 clean'><img alt = '商品预览图' class='book_img' src='../Book_Image/{1}' /></div>" +
                        "<div class='col-md-8'><div class='row'><div class='col-md-offset-1'>" +
                        "<h2><font color='#000'>《{2}》</font></h2><hr class='bg - info'>" +
                                    "<blockquote class='bg-warning text-danger'>" +
                                       "<h3 class='text-primary'>金额：<font size = '5' color='#D9534F'>￥{4}</font ><font color='#D5D5D5' size='3'> x {5}</font></h3>" +
                                       "<h3 class='text-primary'>下单时间：<font size = '5' color='#D9534F'>{8}</font></h3>" +
                                       "<h3 class='text-primary'>状态：<font size = '5' color='#D9534F' class='state'>"+stateDao.GetOrderStateById(order.State) +"</font></h3>" +
                                       "</blockquote>" +
                                    "<div class='row'>" +
                                        "<div class='col-md-12'>" +
                                        "<div class='row'>" +
                                        "<div class='col-md-6' style='height:68px;'><font size = '5' > 合 计：</font><font size = '7' color='#D9534F'>{7}</font ></div>" +
                                        "<div class='col-md-6 text-right content' style='height:68px;padding-top:22px'><span style='display:none' >{0}</span><input type='button' class='btn btn-danger orderstate' value='{9}'/></div>" +
                                        "</div></div></div>" +
                                    "<br/>" +
                                    "<div" +
                                        "<lable class='text-danger' id='errorInfo' value=''></label>" +
                                    "</div>" +
                                "</div>" +
                            "</div>" +
                       " </div>" +
                    "</div>" +
                "</div>", order.Id, order.Book.Image, order.Book.Name, order.Book.Oneword, order.Book.Price, order.Count, order.Book.Id, order.Book.Price * order.Count,order.OrderTime,order.State);
            }
            // 返回给浏览器
            OrderInfor = sbBuilder.ToString();
        }
        else
        {
            EmptyInfo.Text = "请求失败";
            //Response.Redirect("Cart.aspx");
        }
    }


    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        String stateInfo = OrderState.Text;
        int orderId = Convert.ToInt32(TextOrderId.Text);
        if ("取消订单".Equals(stateInfo)) {
            if (OrderDao.UpdateOrderById(orderId, 1))
            {
                EmptyInfo.Text = "取消订单成功！";
            }
            else
            {
                EmptyInfo.Text = "取消订单失败！";
            }
        }
        if ( "确认收货".Equals(stateInfo)) {
            if (OrderDao.UpdateOrderById(orderId, 5))
            {
                EmptyInfo.Text = "收货成功，交易已完成！";
            }
            else {
                EmptyInfo.Text = "确认收货失败！";
            }
        }
        if ("删除订单".Equals(stateInfo))
        {
            if (OrderDao.DeleteOrderById(orderId))
            {
                EmptyInfo.Text = "删除成功！"; 
            }
            else
            {
                EmptyInfo.Text = "删除失败！";
            }
        }
        // 刷新当前页
        Response.AddHeader("Refresh", "0");
    }
}